﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class AddNewCity : System.Web.UI.Page
{
    SqlCommand cmd;
    protected void Page_Load(object sender, EventArgs e)
    {
        //Check user login or not
        if (Session["Username"] == null)
        {
            Response.Redirect("Login.aspx");
        }
        else
            if (Session["RoleName"].ToString() == "Employee")
            {
                Response.Redirect("ValidateUser.aspx");
            }
    }
    protected void imgbAdd_Click(object sender, ImageClickEventArgs e)
    {
        
        SqlConnection cnn = new SqlConnection(ConfigurationManager.ConnectionStrings["OPOConnectionString"].ToString());
        cnn.Open();
        cmd = new SqlCommand("Select CityName from City where CityName like '" + txtCName.Text + "'", cnn);
        SqlDataReader dread = cmd.ExecuteReader();
        if (dread.Read())
        {
            ScriptManager.RegisterStartupScript(this, this.GetType(), "key", "check()", true);
        }
        else
        {
            dread.Close();
            cmd = new SqlCommand("Insert into City (CityName, ZipCode) values (@CityName, @ZipCode)", cnn);

            cmd.Parameters.Add(new SqlParameter("@CityName", SqlDbType.NVarChar, 50));
            cmd.Parameters["@CityName"].Value = txtCName.Text;
            cmd.Parameters.Add(new SqlParameter("@ZipCode", SqlDbType.NVarChar, 10));
            cmd.Parameters["@ZipCode"].Value = txtZipCode.Text;

            cmd.ExecuteNonQuery();

            ScriptManager.RegisterStartupScript(this, this.GetType(), "key", "ShowMessage()", true);
        }
    }
    protected void imgbReset_Click(object sender, ImageClickEventArgs e)
    {
        txtCName.Text = "";
        txtZipCode.Text = "";
    }
    protected void imgbCancel_Click(object sender, ImageClickEventArgs e)
    {
        Response.Redirect("AdminHomePage.aspx");
    }
}